//package com.le.tester.concurrentchallenge.synchronous;
//
//import java.util.concurrent.BrokenBarrierException;
//import java.util.concurrent.CyclicBarrier;
//
///**
// * createTime：2025-03-26 11:09
// * description：
// */
//public class CellularAutomata {
//
//    private final Board mainBoard;
//
//    private final CyclicBarrier barrier;
//
//    private final Worker[] workers;
//
//    public CellularAutomata(Board mainBoard, CyclicBarrier barrier, Worker[] workers) {
//        this.mainBoard = mainBoard;
//        int count = Runtime.getRuntime().availableProcessors();
//        this.barrier = new CyclicBarrier(count, () -> mainBoard.commitNewValues());
//        this.workers = new Worker[count];
//        for (int i = 0; i < count; i++) {
//            workers[i] = new Worker(mainBoard, getSubBoard(count, i));
//        }
//    }
//
//    public class Worker implements Runnable {
//
//        private final Board board;
//
//        public Worker(Board board) {
//            this.board = board;
//        }
//
//        @Override
//        public void run() {
//            while (board.hasCoverged()) {
//                for (int i = 0; i < board.getMaxX(); i++) {
//                    for (int j = 0; j < board.getMaxY(); j++) {
//                        board.setNewValue(i, j, computeValue(i, j));
//                    }
//                }
//                try {
//                    barrier.await();
//                } catch (InterruptedException | BrokenBarrierException e) {
//                    return;
//                }
//
//            }
//
//        }
//    }
//
//    public void start() {
//        for (int i = 0; i < workers.length; i++) {
//            new Thread(workers[i]).start();
//        }
//        mainBoard.waitForConvergence();
//    }
//}
